package sge.servlets;

import java.io.IOException;
import java.util.List;


import javax.jdo.PersistenceManager;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import sge.beans.Fachada;
import sge.beans.AreaProfissional;

import sge.beans.Usuario;
import sge.data.PMF;

public class AreaProfissionalServlet extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
			
		String acao = recuperarAcao(req);
		String nomeArea = req.getParameter("area");
		Fachada fachada = Fachada.obterInstancia();
		
		AreaProfissional area = new AreaProfissional();
		if(acao.equalsIgnoreCase("cadastrar")){
			area.setNome(nomeArea);
			fachada.cadastrarAreaProfissional(area);
		}
		if(acao.equalsIgnoreCase("alterar")){
			Long id  = Long.parseLong(req.getParameter("id"));
			List<AreaProfissional> areas = fachada.listarAreaProfissional();
			PersistenceManager pm =  PMF.get().getPersistenceManager();
			for(int i = 0; i<areas.size();i++){
				if(id == areas.get(i).getIdAreaProfissional())
					try {
				        AreaProfissional e = pm.getObjectById(AreaProfissional.class, areas.get(i).getIdAreaProfissional());
				        e.setNome(nomeArea);			      
				    } finally {
				        pm.close();
				    }
				}
			}
		direcionarPagina(req, resp);
	}
	private String recuperarAcao(HttpServletRequest req) {
		return req.getParameter("acao");
	}
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		Long ID = Long.parseLong(req.getParameter("id"));
		Fachada fachada = Fachada.obterInstancia();
		List<AreaProfissional> area = fachada.listarAreaProfissional();
		AreaProfissional areaP = new AreaProfissional();
		for (int i=0;i<area.size();i++){
			if(area.get(i).getIdAreaProfissional()==ID){
				areaP = area.get(i);
			}
		}
		req.setAttribute("area", areaP);
		alterarRedirecionar(req, resp);
	}
	private void alterarRedirecionar(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		Usuario usuario =  (Usuario) request.getSession().getAttribute("usuario");
		if(usuario.getTipo().equalsIgnoreCase("professor")){
			request.getRequestDispatcher(request.getContextPath()+"professor/area/alterar_area.jsp").forward(request, response);
			return;
		}
		if(usuario.getTipo().equalsIgnoreCase("coordenador")){
			request.getRequestDispatcher(request.getContextPath()+"coordenador/area/alterar_area.jsp").forward(request, response);
			return;
		}
		if(usuario.getTipo().equalsIgnoreCase("administrador")){
			request.getRequestDispatcher(request.getContextPath()+"administrador/area/alterar_area.jsp").forward(request, response);
			return;
		}	
		
	}
	private void direcionarPagina(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		Usuario usuario =  (Usuario) request.getSession().getAttribute("usuario");
		if(usuario.getTipo().equalsIgnoreCase("professor")){
			request.getRequestDispatcher(request.getContextPath()+"professor/feedback/cadastro_efetuado.jsp").forward(request, response);
			return;
		}
		if(usuario.getTipo().equalsIgnoreCase("coordenador")){
			request.getRequestDispatcher(request.getContextPath()+"coordenador/feedback/cadastro_efetuado.jsp").forward(request, response);
			return;
		}
		if(usuario.getTipo().equalsIgnoreCase("administrador")){
			request.getRequestDispatcher(request.getContextPath()+"administrador/feedback/cadastro_efetuado.jsp").forward(request, response);
			return;
		}			
	}

}
